package com.example.demo.use_demo.a_mybatisplus_demo.handler;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.demo.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author farben
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("MetaObjectHandler start insert fill ......");
        this.strictInsertFill(metaObject, "createUserId", Long.class, TokenUtil.getUserId());
        this.strictInsertFill(metaObject, "createTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "updateUserId", Long.class, TokenUtil.getUserId());
        this.strictInsertFill(metaObject, "updateTime", Date.class, DateTime.now());
        this.strictInsertFill(metaObject, "tenantId", Long.class, TokenUtil.getTenantId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("MetaObjectHandler start update fill ......");
        this.strictUpdateFill(metaObject, "updateUserId", Long.class, TokenUtil.getUserId());
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateTime.now());
    }

}
